<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/31 0031
 * Time: 10:16
 */

namespace app\api\controller\v1;


use app\api\validate\Count;
use app\api\model\Product as ProductModel;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\ProductException;

class Product
{
    public function getRecent($count=15){
        (new Count())->goCheck();
        $products = ProductModel::getMostRecent($count);
        if ($products->isEmpty()){
            throw new ProductException();
        }
        /*//        得到数据集对象(一组模型对象) -----在database.php中46行修改collection，这里就不需要写了
        $collection = collection($products);
        $products = $collection->hidden(['summary']);*/
        $products = $products->hidden(['summary']);
        return $products;
    }
    public function getAllInCategory($id){
        (new IDMustBePositiveInt())->goCheck();
        $products = ProductModel::getProductsByCategoryID($id);
        if ($products->isEmpty()){
            throw new ProductException();
        }
        $products = $products->hidden(['summary']);
        return $products;
    }
    public function getOne($id){
        (new IDMustBePositiveInt())->goCheck();
        $products = ProductModel::getProductDetail($id);
        if (!$products){
            throw new ProductException();
        }
        return $products;
    }
}